<?php

namespace frontend\modules\mongodb\controllers;

use yii;
use yii\web\Controller;

/**
 * Default controller for the `mongodb` module
 */
class LbsController extends Controller
{
    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex()
    {
        p(['hello world']);
    }

    /**
     * 创建数据
     */
    public function actionCreate()
    {
        $mongodb = Yii::$app->mongodb->getCollection ('placess');
        $db = new yii\db\Connection([
            'dsn' => 'mysql:host=120.25.154.227;dbname=hello',
            'username' => 'hello',
            'password' => 'hello2017',
            'charset' => 'utf8',
            'tablePrefix' => 'me_'
        ]);
        $sql = "select * from me_geolocation order by id asc";
        $list = $db->createCommand($sql)->queryAll();
        
        $data = [];
        $res = [];
        foreach ($list as $key => $val) {
            $data['coordinate'][] = (float)$val['longitude'];
            $data['coordinate'][] = (float)$val['latitude'];
            $data['title'] = $val['name'];
            $data['address'] = $val['name'];
            $data['id'] = $val['id'];
            
            $res[] = $mongodb->save($data);
            
            $data = [];
        }
        p($res);
    }
    
    /**
     * 创建索引
     */
    public function actionCreateIndex()
    {
        $mongodb = Yii::$app->mongodb->getCollection ('places');
        $res = $mongodb->createIndex(['coordinate','status' => '2d']);
        p($res);
    }
    
    /**
     * 显示索引
     */
    public function actionListIndex()
    {
        $mongodb = Yii::$app->mongodb->getCollection ('places');
        $res = $mongodb->listIndexes();
        p($res);
    }
    
    
    
    
    
    
    
    
}
